﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DCSoft.TemperatureChart
{
    [System.Runtime.InteropServices.ComVisible(false)]
    public partial class dlgTCOptions : Form
    {
        public dlgTCOptions()
        {
            InitializeComponent();
            this.DialogResult = DialogResult.Cancel;
        }

        private TemperatureDocumentOptions _OptionsInstance = null;
        /// <summary>
        /// 选项信息对象
        /// </summary>
        public TemperatureDocumentOptions OptionsInstance
        {
            get
            {
                return _OptionsInstance; 
            }
            set
            {
                _OptionsInstance = value; 
            }
        }
        /// <summary>
        /// 加载选项信息对象数据至窗体控件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            ((DataGridViewComboBoxColumn)this.dgvHeaderLines.Columns["ValueType"]).DataSource = Enum.GetValues(typeof(TitleLineValueType));
            ((DataGridViewComboBoxColumn)this.dgvYAxisInfos.Columns["ValueMember"]).DataSource = Enum.GetValues(typeof(ValueMemberType));
            ((DataGridViewComboBoxColumn)this.dgvYAxisInfos.Columns["SymbolStyle"]).DataSource = Enum.GetValues(typeof(PointSymbolStyle));
            ((DataGridViewComboBoxColumn)this.dgvFooterLines.Columns["ValueTypeH"]).DataSource = Enum.GetValues(typeof(TitleLineValueType));
            //((DataGridViewComboBoxColumn)this.dgvFooterLines.Columns["
            this.txtBackColor.Text = _OptionsInstance.BackColor.Name;
            this.txtDateFormatString.Text = _OptionsInstance.DateFormatString;
            this.txtFontName.Text = _OptionsInstance.FontName;
            StringBuilder hourticks = new StringBuilder();
            foreach (int tick in _OptionsInstance.HourTicks)
            {
                hourticks.Append(tick).Append(",");
            }
            this.txtHourTicks.Text = hourticks.ToString();
            this.txtNumOfDaysInOnePage.Text = _OptionsInstance.NumOfDaysInOnePage.ToString();
            this.txtSymbolSize.Text = _OptionsInstance.SymbolSize.ToString();
            this.txtYSplitNum.Text = _OptionsInstance.YSplitNum.ToString();

            this.dgvHeaderLines.DataSource = _OptionsInstance.HeaderLines;
            this.dgvHeaderLines.Columns["StartDate"].Visible = false;
            this.dgvYAxisInfos.DataSource = _OptionsInstance.YAxisInfos;
            this.dgvFooterLines.DataSource = _OptionsInstance.FooterLines;
            this.dgvFooterLines.Columns["StartDateKeyword"].Visible = false;
            this.dgvFooterLines.Columns["StartDate"].Visible = false;

        }

        private void dlgTCOptions_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 保存窗体控件信息至选项信息对象
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {

            #region 时刻数序列
            if (this.txtHourTicks.Text != null && txtHourTicks.Text != string.Empty)
            {
                string[] hourst = this.txtHourTicks.Text.Split(',');
                List<string> hourstlist = new List<string>();
                foreach (string hour in hourst)
                {
                    if (hour != null && hour != string.Empty)
                    {
                        hourstlist.Add(hour);
                    }
                }
                int[] hoursticks = new int[hourstlist.Count];
                for (int i = 0; i < hourstlist.Count; i++)
                {
                    hoursticks.SetValue(Convert.ToInt32(hourstlist[i]), i);
                }
                _OptionsInstance.HourTicks = hoursticks;
            }
            else
            {
                _OptionsInstance.HourTicks = new TemperatureDocumentOptions().HourTicks;
            }
            #endregion

            _OptionsInstance.BackColor = Color.FromName(txtBackColor.Text);//背景色
            _OptionsInstance.DateFormatString = txtDateFormatString.Text;//日期格式化字符串
            _OptionsInstance.FontName = txtFontName.Text;//字体名称
            _OptionsInstance.NumOfDaysInOnePage = Convert.ToInt32(txtNumOfDaysInOnePage.Text);//每页显示的天数
            _OptionsInstance.SymbolSize = float.Parse(txtSymbolSize.Text);//数据点符号大小
            _OptionsInstance.YSplitNum = Convert.ToInt32(txtYSplitNum.Text);//Y轴分割区域数

            _OptionsInstance.HeaderLines = ((List<TitleLineInfo>)this.dgvHeaderLines.DataSource);
            _OptionsInstance.YAxisInfos = ((YAxisInfoList)this.dgvYAxisInfos.DataSource);
            _OptionsInstance.FooterLines = ((List<TitleLineInfo>)this.dgvFooterLines.DataSource);

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvYAxisInfos_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if(this.dgvYAxisInfos.Columns[e.ColumnIndex].HeaderText=="颜色")
            {
                ColorDialog colorDialog = new ColorDialog();
                colorDialog.AllowFullOpen = true;
                colorDialog.FullOpen = true; colorDialog.ShowHelp = true;
                colorDialog.ShowDialog();
                Color cl = colorDialog.Color;
                dgvYAxisInfos.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = cl.Name.ToString();
            }
            
        }
    }
}
